<template>
	<view class="view">
		<up-navbar
			leftText="返回"
			:rightText="phoneLogin ? '密码登录' : '验证码登录'"
			title="登录"
			:placeholder="true"
			:safeAreaInsetTop="true"
			@leftClick="back"
			@rightClick="phoneLogin = !phoneLogin"
		></up-navbar>
		<view class="box">
			<uni-title type="h1" title="应聘者登录"></uni-title>
		</view>
		<!-- 密码登录 -->
		<view class="input" v-if="!phoneLogin">
			<up-input v-model="phone" prefixIcon="phone" prefixIconStyle="font-size: 22px;color: #909399" placeholder="请输入11位手机号" border="bottom" clearable></up-input>
			<br />
			<up-input
				v-model="password"
				type="password"
				prefixIcon="lock"
				prefixIconStyle="font-size: 22px;color: #909399"
				placeholder="请输入密码"
				border="bottom"
				clearable
			></up-input>
			<view class="more">
				<u-text text="立即注册" @click="register"></u-text>
				<br />
				<u-text text="忘记密码"></u-text>
			</view>
		</view>
		<!-- 验证码登录 -->
		<view v-else class="input">
			<up-input v-model="phone" prefixIcon="phone" prefixIconStyle="font-size: 22px;color: #909399" placeholder="请输入11位手机号" border="bottom" clearable></up-input>
			<br />
			<view class="u-demo-block__content">
				<!-- #ifndef APP-NVUE -->
				<up-input prefixIcon="list" v-model="code" placeholder="请输入验证码" border="bottom" clearable>
					<!-- #endif -->
					<!-- #ifdef APP-NVUE -->
					<up-input placeholder="请输入验证码">
						<!-- #endif -->
						<template #suffix>
							<up-code ref="uCode" @change="codeChange" seconds="20" changeText="X秒后重新获取"></up-code>
							<up-button @tap="captcha" :text="tips" type="primary" size="mini"></up-button>
						</template>
						<!-- #ifndef APP-NVUE -->
					</up-input>
					<!-- #endif -->
					<!-- #ifdef APP-NVUE -->
				</up-input>
				<!-- #endif -->
			</view>
			<view class="more">
				<u-text text="登录时遇到问题？"></u-text>
				<br />
				<u-text text=""></u-text>
			</view>
		</view>
		<up-button @click="login" style="margin-top: 100px; width: 150px" type="primary" text="登录"></up-button>
		<!-- 顶部消息弹窗 -->
		<transition name="fade">
			<view v-if="showNotification" class="notification" @click="showNotification = false">
				<view class="notification-icon">
					<up-icon name="chat-fill" size="24" color="#fff"></up-icon>
				</view>
				<view class="notification-content">
					<text class="notification-title">{{ notificationTitle }}</text>
					<br />
					<text class="notification-message">{{ notificationMessage }}</text>
				</view>
			</view>
		</transition>
		<!-- 滑块验证码 -->
		<Vcode :imgs="[img1, img2]" :show="isShow" @success="onSuccess" />
	</view>
</template>

<script>
import Vcode from 'vue3-puzzle-vcode';

export default {
	data() {
		return {
			phone: '',
			password: '',
			code: '',
			phoneLogin: false,
			tips: '',
			value: '',
			showNotification: false,
			notificationTitle: '1900023563203921039',
			notificationMessage: '',
			isShow: false,
			img1: this.getRandomImg(),
			img2: this.getRandomImg()
		};
	},
	components: {
		Vcode
	},
	onLoad() {
		this.$api.getUUID().then((res) => {
			uni.setStorageSync('uuid', res);
		});
	},
	methods: {
		// 返回欢迎页
		back() {
			uni.redirectTo({
				url: '/pages/index/index'
			});
		},
		// 登录逻辑
		login() {
			if (this.validateForm()) {
				if (this.phoneLogin) {
					this.$api
						.loginCode({
							account: this.phone,
							code: this.code
						})
						.then((res) => {
							uni.showToast({
								title: `${res.message}`,
								icon: 'none'
							});
							if (res.data != null) {
								this.code = '';
								uni.setStorageSync('user', res.data);
								uni.setStorageSync('token', res.data.token);
								uni.navigateTo({
									url: '/pages/view/view'
								});
							}
						});
				} else {
					this.$api
						.login({
							account: this.phone,
							password: this.password
						})
						.then((res) => {
							this.code = '';
							uni.showToast({
								title: `${res.message}`,
								icon: 'none'
							});
							if (res.data != null) {
								this.code = '';
								uni.setStorageSync('user', res.data);
								uni.setStorageSync('token', res.data.token);
								uni.navigateTo({
									url: '/pages/view/view'
								});
							}
						});
				}
			}
		},
		// 前往注册页
		register() {
			uni.navigateTo({
				url: '/pages/index/register'
			});
		},
		// 验证表单
		validateForm() {
			if (!this.phone) {
				uni.showToast({
					title: '请输入手机号',
					icon: 'none'
				});
				return false;
			}
			if (!/^1[3-9]\d{9}$/.test(this.phone)) {
				uni.showToast({
					title: '手机号格式不正确',
					icon: 'none'
				});
				return false;
			}
			if (this.phoneLogin) {
				if (!this.code) {
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					});
					return false;
				}
				return true;
			} else {
				if (!this.password) {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					});
					return false;
				}
				return true;
			}
		},
		codeChange(text) {
			this.tips = text;
		},
		// 验证码获取
		getCode() {
			if (this.$refs.uCode.canGetCode) {
				uni.showLoading({
					title: '正在获取验证码'
				});
				setTimeout(() => {
					uni.hideLoading();
					uni.$u.toast('验证码已发送');
					this.$api.getCode().then((res) => {
						this.notificationMessage = `【人才招聘】尊敬的${this.phone}，您的验证码为${res}，5分钟内有效，请勿外泄。`;
						this.showNotification = true;
					});
					this.$refs.uCode.start();
				}, 2000);
				setTimeout(() => {
					this.showNotification = false;
				}, 10000);
			} else {
				uni.$u.toast('倒计时结束后再发送');
			}
		},
		// 显示滑块验证
		captcha() {
			if (this.$refs.uCode.canGetCode) {
				if (!this.phone) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					});
					return;
				}
				if (!/^1[3-9]\d{9}$/.test(this.phone)) {
					uni.showToast({
						title: '手机号格式不正确',
						icon: 'none'
					});
					return;
				}
				this.isShow = true;
			} else {
				uni.$u.toast('倒计时结束后再发送');
			}
		},
		// 滑块验证完成
		onSuccess() {
			this.isShow = false;
			this.getCode();
		},
		// 范围随机数
		getRandomNumberByRange(start, end) {
			return Math.round(Math.random() * (end - start) + start);
		},
		// 随机图片
		getRandomImg() {
			return 'https://picsum.photos/300/150/?image=' + this.getRandomNumberByRange(0, 100);
		}
	}
};
</script>

<style>
.view {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	position: relative;
}
.box {
	/* #ifndef APP-NVUE */
	display: block;
	/* #endif */
	margin-top: 40px;
	margin-bottom: 80px;
}
.input {
	width: 350px;
}
.more {
	margin-top: 20px;
	display: flex;
	justify-content: space-between;
}

.notification {
	position: fixed;
	top: 0;
	left: 50%;
	transform: translateX(-50%);
	background-color: rgba(64, 158, 255, 0.9); /* 添加透明度 */
	color: #fff;
	display: flex;
	align-items: center;
	padding: 10px 20px;
	border-radius: 8px;
	box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
	z-index: 1000;
	width: 90%;
	max-width: 400px;
	margin-top: 10px;
}

.notification-icon {
	margin-right: 10px;
}

.notification-content {
	flex: 1;
}

.notification-title {
	font-weight: bold;
	font-size: 16px;
}

.notification-message {
	margin-top: 5px;
	font-size: 14px;
}

.fade-enter-active,
.fade-leave-active {
	transition: opacity 0.5s;
}

.fade-enter,
.fade-leave-to {
	opacity: 0;
}

.u-text[data-v-0a574502] {
	flex: none;
	width: 60px;
}
</style>
